/*
    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) <year>  <name of author>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "smcontrol.h"
#include <iostream>
#include <sstream>
#include <cstdlib>

using namespace std;


void SMcontrol::addLP(string lpClass, string meClass, string adClass, CORBA::ULong id, string neighbors)
{
    LPtype* tempLP=new LPtype();
    tempLP->lpClass=lpClass;
    tempLP->meClass=meClass;
    tempLP->adClass=adClass;
    tempLP->neighbors=neighbors;
    FILE_LOG(logDEBUG)<<"adding LP:"<<lpClass<<"|"<<meClass<<"|"<<adClass<<"|"<<id<<"|"<<neighbors;
    _lps[id]=tempLP;
}
void SMcontrol::initSM(AddressServer::mcp_var ASmcp)
{
    FILE_LOG(logINFO)<<"Creating new SM"<<endl;
    ::CORBA::ULong SMid;
    _siorSetup=ASmcp->allocateSM(SMid);

    if (SMid==0 && !strcmp(_siorSetup,"empty"))
    {
        FILE_LOG(logINFO) <<"No SM available at the AS-Server"<<endl;
        exit(0);
    }
    else
    {
        FILE_LOG(logDEBUG)<<"Received SM: "<<SMid<<" with address:"<<_siorSetup<<endl;

        FILE_LOG(logDEBUG)<<"string to object...";
        CORBA::Object_var obj_var=mycorba::instance().getORB()->string_to_object (_siorSetup);
        FILE_LOG(logDEBUG)<<"string to object done:"<<_siorSetup;
        if (CORBA::is_nil(obj_var))
        {
            FILE_LOG(logDEBUG)<<"NIL1 refference received";
            exit(0);
        }
        FILE_LOG(logDEBUG)<<"Casting to SMSetup ...";
        SMInterface::SMSetup_var SMsetup=SMInterface::SMSetup::_narrow(obj_var);
        FILE_LOG(logDEBUG)<<"Casting to SMSetup done";
        if (CORBA::is_nil(SMsetup))
        {
            FILE_LOG(logDEBUG)<<"NIL2 refference received";
            exit(0);
        }
        FILE_LOG(logDEBUG)<<"Trying to create Scheduler";
        //SMsetup->run();
        SMsetup->initSM(_id,"corbaAddressServer");
        SMsetup->createScheduler(const_cast<char *>(_schedClass.c_str()),_endGVT);
        FILE_LOG(logDEBUG)<<"Created new Scheduler";
        _siorRun =SMsetup->getSMAddress();
        FILE_LOG(logDEBUG) <<"smrun interface:"<<_siorRun;
        lpMapTyp::iterator lpit=_lps.begin();
        while (lpit!=_lps.end())
        {

            LPtype* temp=lpit->second;
            FILE_LOG(logDEBUG)<<"creating LP"<<lpit->first;
            SMsetup->createLP(lpit->first,
                              const_cast<char *>(temp->neighbors.c_str()),
                              const_cast<char *>(temp->meClass.c_str()),
                              const_cast<char *>(temp->lpClass.c_str()),
                              const_cast<char *>(temp->adClass.c_str())
                             );
            lpit++;
        }
    }
}


char* SMcontrol::getsiorSetup()
{
    return _siorSetup;
}


char* SMcontrol::getsiorRun()
{
    return _siorRun;
}
